package com.example.demo.module.supplier.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.entity.response.ResCode;
import com.example.demo.common.entity.response.Rs;
import com.example.demo.constant.Constant;
import com.example.demo.entity.pojo.supplier.SupplierPoJo;
import com.example.demo.entity.request.supplier.SupplierRequest;
import com.example.demo.entity.response.supplier.SupplierResponse;
import com.example.demo.module.supplier.service.SupplierService;
import com.example.demo.utils.BankCardUtil;
import com.example.demo.utils.CommonUtils;
import com.example.demo.utils.TelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 供应商控制层.
 *
 * @author Mei Ruoxiao
 * @since 2020/9/16
 */
@Slf4j
@RestController("supplierc")
@RequestMapping("/supplier")
@Api(tags = "供应商接口")
public class SupplierController {

    @Resource
    private SupplierService service;

    @PostMapping
    @ApiOperation(value = "新增供应商")
    public ResponseEntity<Rs<Object>> create(
            @Valid @RequestBody SupplierRequest request,
            @ApiIgnore @RequestParam(Constant.HeaderKey.COMPANY_ID) long companyId) {
        log.info("create:新增供应商[request:{},companyId:{}]", request, companyId);
        //校验电话
        if (!TelUtil.isTel(request.getTel())) {
            return Rs.fail(ResCode.COMPANY_TEL_IS_INVALID);
        }
        //校验银行卡
        if (!BankCardUtil.checkBankCard(request.getAccount())) {
            return Rs.fail(ResCode.SUPPLIER_BANK_NUM__IS_INVALID);
        } else {
            String bankName = BankCardUtil.getNameOfBank(request.getAccount());
            log.info("create:所属银行卡[bankName:{}]", bankName);
            if (!Objects.equals(bankName, request.getBank())) {
                return Rs.fail(ResCode.SUPPLIER_BANK_NAME__ERROR);
            }
        }
        if (Objects.nonNull(service.getOne(Wrappers.<SupplierPoJo>lambdaQuery()
                .eq(SupplierPoJo::getAccount, request.getAccount())
                .eq(SupplierPoJo::getName, request.getName())
                .eq(SupplierPoJo::getCompanyId, companyId)))) {
            log.warn("update:[数据不存在]");
            return Rs.fail(ResCode.API_FAIL_EXEC_ADD_EXIST);
        }
        return Rs.ok(CommonUtils.getIdResponse(service.create(request, companyId)));
    }

    /**
     * 获取供应商列表
     *
     * @author Mei Ruoxiao
     * @since 2020/9/16
     */
    @GetMapping("/list")
    @ApiOperation("获取供应商列表")
    public ResponseEntity<Rs<Page<SupplierResponse>>> page(
            @ApiParam("查询条件-企业名称") @RequestParam(value = "name", required = false) String name,
            @ApiParam("查询条件-联系人") @RequestParam(value = "boss", required = false) String boss,
            @ApiParam("查询条件-联系人电话") @RequestParam(value = "tel", required = false) String tel,
            @ApiParam("页号,从1开始,当page=size=-1时,表示不分页")
            @RequestParam(value = "page", required = false, defaultValue = "1") final int page,
            @RequestParam(value = "size", required = false, defaultValue = "10") final int size,
            @ApiIgnore @RequestParam(value = Constant.HeaderKey.COMPANY_ID) final long companyId) {
        log.info("page:获取供应商列表[name:{}, boss:{}, tel:{}, page:{}, size:{}, companyId:{}]", name, boss, tel, page, size, companyId);
        Page<SupplierPoJo> poJoPage = service.page(name, boss, tel, page, size, companyId);
        Page<SupplierResponse> responsePage = CommonUtils.toPage(poJoPage, SupplierResponse::of);
        return Rs.ok(CommonUtils.getResponse(responsePage));
    }
}
